package com.group.lyl.send.util;

import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

import java.io.*;
import java.util.ArrayList;
import java.util.List;

/**
 * 读写文件工具类
 */
@Component
public class ReadFile {




    @Value("${file.path}")
    private   String path; //文件路径

    private final static String SEPARATOR =","; //字段分隔符


    public  String getSeparator(){
        return SEPARATOR;
    }


    /**
     * 读取文件
     * @return
     */
    public List<String> readFile(){


        String root = System.getProperty("user.dir");//获取项目路径
        File file = new File(root+path);

        StringBuilder result = new StringBuilder();

        List<String> list = new ArrayList<>();
        try{


            BufferedReader br = new BufferedReader(new InputStreamReader(new FileInputStream(file), "UTF-8"));//构造一个BufferedReader类来读取文件

            String s = null;
            //一次读取一行
            while((s = br.readLine())!=null){
                list.add(s);
            }
            br.close();
        }catch(Exception e){
            e.printStackTrace();
        }
        return list;

    }

    /**
     * 根据字段位置读取文件
     * @param key
     * @param value
     * @return
     */
    public  List<String> readFile(int key,String value){

        try {
            List<String> list = readFile();
            List<String> result = new ArrayList<>();//装结果集
            for (String s : list) {
                String[] split = s.split(SEPARATOR);
                if (value.equals(split[key])) {
                     result.add(s);
                }
            }
            return result;
        }catch (Exception e){
            e.getMessage();
            return null;
        }

    }

    /**
     * 根据字段名读取文件
     * @param key
     * @param value
     * @return
     */
    public  List<String> readFile(String key,String value){
        try {
            List<String> list = readFile();

            String fileds = list.get(0);//获取第一段字段值

            String[] filedsArray = fileds.split(SEPARATOR);

            int k = -1;//字段位置

            for (int i = 0; i < filedsArray.length; i++) {
                if(key.equals(filedsArray[i])){
                    k = i;
                }

            }
            if(k == -1){

                return null;
            }
            else{
                return readFile(k,value);
            }

        }catch (Exception e){
            e.getMessage();
            return null;
        }
    }

    /**
     * 储存文件
     * @param value
     */
    public  void saveFile(String value){

        try {
            String root = System.getProperty("user.dir");
            File file = new File(root+path);
            BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(
                    new FileOutputStream(file, true)));
           // FileOutputStream out = new FileOutputStream(file);
           // OutputStreamWriter outWriter = new OutputStreamWriter(out, "UTF-8");


            bufferedWriter.write(value + "\r\n");

            bufferedWriter.close();

           // out.close();
        } catch (Exception e) {
            e.printStackTrace();

        }


    }

}
